package com.sitia.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.sitia.common.CustomException;
import com.sitia.common.constant.UserConstants;
import com.sitia.common.utils.DateUtils;
import com.sitia.common.utils.SecurityUtils;
import com.sitia.common.utils.StringUtils;
import com.sitia.system.domain.MxCity;
import com.sitia.system.domain.MxProvince;
import com.sitia.system.domain.SysCustomer;
import com.sitia.system.mapper.MxCityMapper;
import com.sitia.system.mapper.MxProvinceMapper;
import com.sitia.system.mapper.SysCustomerMapper;
import com.sitia.system.service.ISysCustomerService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户Service业务层处理
 * 
 * @author sitia
 * @date 2025-06-23
 */
@Service
public class SysCustomerServiceImpl implements ISysCustomerService
{
    @Resource
    private SysCustomerMapper sysCustomerMapper;
    @Resource
    private MxProvinceMapper provinceMapper;
    @Resource
    private MxCityMapper cityMapper;

    /**
     * 查询客户
     * 
     * @param customerId 客户主键
     * @return 客户
     */
    @Override
    public SysCustomer selectSysCustomerByCustomerId(String customerId)
    {
        return sysCustomerMapper.selectSysCustomerByCustomerId(customerId);
    }

    /**
     * 查询客户列表
     * 
     * @param sysCustomer 客户
     * @return 客户
     */
    @Override
    public List<SysCustomer> selectSysCustomerList(SysCustomer sysCustomer)
    {
        return sysCustomerMapper.selectSysCustomerList(sysCustomer);
    }

    /**
     * 新增客户
     * 
     * @param sysCustomer 客户
     * @return 结果
     */
    @Override
    public int insertSysCustomer(SysCustomer sysCustomer)
    {
        return sysCustomerMapper.insertSysCustomer(sysCustomer);
    }

    /**
     * 修改客户
     * 
     * @param sysCustomer 客户
     * @return 结果
     */
    @Override
    public int updateSysCustomer(SysCustomer sysCustomer)
    {
        return sysCustomerMapper.updateSysCustomer(sysCustomer);
    }

    /**
     * 批量删除客户
     * 
     * @param customerIds 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteSysCustomerByCustomerIds(String[] customerIds)
    {
        return sysCustomerMapper.deleteSysCustomerByCustomerIds(customerIds);
    }

    /**
     * 删除客户信息
     * 
     * @param customerId 客户主键
     * @return 结果
     */
    @Override
    public int deleteSysCustomerByCustomerId(String customerId)
    {
        return sysCustomerMapper.deleteSysCustomerByCustomerId(customerId);
    }

    @Override
    public void addSysCustomer(SysCustomer sysCustomer) {
        if (!this.checkUserNameUnique(sysCustomer))
        {
            throw new CustomException("新增客户'" + sysCustomer.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(sysCustomer.getPhoneNum()) && !this.checkPhoneUnique(sysCustomer))
        {
            throw new CustomException("新增客户'" + sysCustomer.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(sysCustomer.getEmail()) && !this.checkEmailUnique(sysCustomer))
        {
            throw new CustomException("新增客户'" + sysCustomer.getUserName() + "'失败，邮箱账号已存在");
        }
        sysCustomer.setCustomerId("CU"+IdUtil.getSnowflakeNextId());
        sysCustomer.setCreatedBy(SecurityUtils.getUserId().toString());
        sysCustomer.setCreatedTime(DateUtils.getNowDate());
        beforeInitUser(sysCustomer);
        sysCustomerMapper.insertSysCustomer(sysCustomer);
    }
    private void beforeInitUser(SysCustomer user) {
        if (StringUtils.isNotEmpty(user.getProvince())) {
            MxProvince mxProvince = provinceMapper.selectMxProvinceByName(user.getProvince());
            if (!ObjectUtils.isEmpty(mxProvince)) {
                user.setProvinceCode(mxProvince.getCode());
            }

        }
        if (StringUtils.isNotEmpty(user.getCity())) {
            MxCity mxCity = cityMapper.selectMxCityByName(user.getCity());
            if (!ObjectUtils.isEmpty(mxCity)) {
                user.setCityCode(mxCity.getCityCode());
            }
        }
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(SysCustomer user)
    {
        SysCustomer info = sysCustomerMapper.checkUserNameUnique(user.getUserName());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(SysCustomer user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysCustomer info = sysCustomerMapper.checkPhoneUnique(user.getPhoneNum());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkEmailUnique(SysCustomer user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        SysCustomer info = sysCustomerMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
